/******************************************************************************
* 	Copyright (c) 2007-2008 All rights reserved
*		Asemantics S.r.l
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer. 
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. The end-user documentation included with the redistribution,
*    if any, must include the following acknowledgment:
*       "This product includes software developed by 
*	 Asemantics S.r.l."
*    Alternately, this acknowledgment may appear in the software itself,
*    if and wherever such third-party acknowledgments normally appear.
*
* 4. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*    This product includes software developed by Asemantics S.r.l.
*    the Semantic Web company, Rome, London, Leiden and its contributors. 
*
* 5. Neither the name of the company nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Author: Andrea Marchesini - baku@asemantics.com
*
******************************************************************************/

#include <configure.h>

struct data_t *
configure_init (int argc, char **argv, GError ** error)
{
  gchar **arguments = NULL;

  GOptionContext *context;
  GOptionEntry entries[] = {
    {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &arguments, NULL,
     NULL},
    {NULL}
  };

  struct data_t *data;

  context = g_option_context_new ("logfile outputdir");
  g_option_context_add_main_entries (context, entries, NULL);

  if (g_option_context_parse (context, &argc, &argv, error) == FALSE)
    {
      g_option_context_free (context);
      return NULL;
    }

  g_option_context_free (context);

  if (!arguments || !arguments[0] || !arguments[1])
    {
      g_set_error (error, log2html_error_quark (), 0,
		   "The logfile and the output directory are required!");

      if (arguments)
	g_strfreev (arguments);
      return NULL;
    }

  data = g_malloc0 (sizeof (struct data_t));

  data->logfile = g_strdup (arguments[0]);
  data->directory = g_strdup (arguments[1]);

  g_strfreev (arguments);

  if (g_file_test (data->directory, G_FILE_TEST_IS_DIR) == FALSE)
    {
      g_set_error (error, log2html_error_quark (), 0,
		   "The directory '%s' doesn't exist.", data->directory);

      configure_free (data);
      return NULL;
    }

  if (g_file_test (data->logfile, G_FILE_TEST_IS_REGULAR) == FALSE)
    {
      g_set_error (error, log2html_error_quark (), 0,
		   "The logfile '%s' doesn't exist.", data->logfile);

      configure_free (data);
      return NULL;
    }

  return data;
}

void
configure_free (struct data_t *data)
{
  if (!data)
    return;

  if (data->logfile)
    g_free (data->logfile);

  if (data->directory)
    g_free (data->directory);

  if (data->logs)
    {
      g_list_foreach (data->logs, (GFunc) configure_free_log, NULL);
      g_list_free (data->logs);
    }

  if (data->html_header)
    g_free (data->html_header);

  if (data->html_footer)
    g_free (data->html_footer);

  g_free (data);
}

void
configure_free_log (struct log_t *log)
{
  if (!log)
    return;

  if (log->istances)
    {
      g_list_foreach (log->istances, (GFunc) configure_free_istance, NULL);
      g_list_free (log->istances);
    }

  if (log->wait_istances)
    {
      g_list_foreach (log->wait_istances, (GFunc) configure_free_wait, NULL);
      g_list_free (log->wait_istances);
    }

  if (log->map)
    g_free (log->map);

  g_free (log);
}

void
configure_free_istance (struct log_istance_t *istance)
{
  if (!istance)
    return;

  switch (istance->type)
    {
    case ISTANCE_BORN:
      if (istance->born.input)
	g_free (istance->born.input);

      break;

    case ISTANCE_DIED:
      if (istance->died.output)
	g_free (istance->died.output);

      break;

    case ISTANCE_WAIT:
      break;

    case ISTANCE_ERROR_EXEC:
      if (istance->error_exec.reason)
	g_free (istance->error_exec.reason);

      if (istance->error_exec.command)
	g_free (istance->error_exec.command);

      break;

    case ISTANCE_ERROR_OUTPUT:
      if (istance->error_output)
	g_free (istance->error_output);

      break;

    case ISTANCE_ERROR_IO:
      if (istance->error_io.operation)
	g_free (istance->error_io.operation);

      if (istance->error_io.error)
	g_free (istance->error_io.error);

      break;

    case ISTANCE_ERROR_PROCESS:
      if (istance->error_process.process)
	g_free (istance->error_process.process);

      if (istance->error_process.error)
	g_free (istance->error_process.error);

      break;

    case ISTANCE_ERROR_PARSER:
      if (istance->error_parser)
	g_free (istance->error_parser);

      break;
    }

  if (istance->process)
    g_free (istance->process);

  g_free (istance);
}

void
configure_free_wait (struct log_wait_t *wait)
{
  if (!wait)
    return;

  g_free (wait);
}

/* EOF */
